package com.fortune.service.impl;

import com.fortune.dao.DStoreDeficitDao;
import com.fortune.dto.in.dStoreDeficit.DStoreDeficitAddDto;
import com.fortune.dto.in.dStoreDeficit.DStoreDeficitEditDto;
import com.fortune.dto.in.dStoreDeficit.DStoreDeficitInfoDto;
import com.fortune.dto.in.dStoreDeficit.DStoreDeficitListDto;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.DStoreDeficitService;
import com.fortune.vo.dStoreDeficit.DStoreDeficitInfo;
import com.fortune.vo.dStoreDeficit.DStoreDeficitInfoDetail;
import com.fortune.vo.mStoreDeficit.MStoreDeficitInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
@Service
public class DStoreDeficitServiceImpl implements DStoreDeficitService {
    @Autowired
    private DStoreDeficitDao dStoreDeficitDao;


    @Override
    public int  addDeficitBase(String companyId, String dealerId, String uid, DStoreDeficitAddDto addDto) {
        String uuid= UUID.randomUUID().toString().replace("-", "");
        int addDeficitBase=dStoreDeficitDao.addDeficitBase(companyId,dealerId,uuid,uid,addDto);
        if(addDeficitBase>0){
            int addDeficitDetail=0;
            for(DStoreDeficitInfoDto infoDto: addDto.getdStoreDeficitInfoDto()){
                addDeficitDetail=dStoreDeficitDao.addDeficitDetail(companyId,dealerId,uuid,infoDto);
                if(addDeficitDetail<1){
                    throw new InsertInnerErrorException("新增报亏商品失败");
                }
            }
            return addDeficitDetail;

        }
        return addDeficitBase;
    }

    @Override
    public List<DStoreDeficitInfo> findDeficitList(String companyId, String dealerId, DStoreDeficitListDto dStoreDeficitListDto) {
        return dStoreDeficitDao.findDeficitList(companyId,dealerId,dStoreDeficitListDto);
    }

    @Override
    public int findDeficitListCount(String companyId, String dealerId, DStoreDeficitListDto dStoreDeficitListDto) {
        return dStoreDeficitDao.findDealerDeficitListCount(companyId,dealerId,dStoreDeficitListDto);
    }

    @Override
    public DStoreDeficitInfoDetail findDetail(String companyId,String dealerId, String deficitId) {
        return dStoreDeficitDao.findDealerDeficitInfo(companyId,dealerId,deficitId);
    }

    @Override
    public int editDStoreDeficitConfirm(String companyId, String dealerId,String operatorId, String uuid) {
        return dStoreDeficitDao.editDStoreDeficitConfirm(companyId,dealerId,operatorId,uuid);
    }

    @Override
    public int deleteDStoreDeficit(String companyId, String dealerId, String uuid) {
        return dStoreDeficitDao.deleteDStoreDeficit(companyId, dealerId, uuid);
    }

    @Override
    public int editDeficitBase(String companyId, String dealerId, DStoreDeficitEditDto editDto) {
        int editDeficitBase=dStoreDeficitDao.editDeficitBase(companyId,dealerId,editDto);
        if(editDeficitBase>0){
            String uuid=editDto.getUuid();
            int addDeficitDetail=0;
            int delete=dStoreDeficitDao.deleteInfo(companyId,dealerId,uuid);
            if(delete>0){
                for(DStoreDeficitInfoDto infoDto: editDto.getdStoreDeficitInfoDto()){
                    addDeficitDetail=dStoreDeficitDao.addDeficitDetail(companyId,dealerId,uuid,infoDto);
                    if(addDeficitDetail<1){
                        throw new InsertInnerErrorException("新增报亏商品失败");
                    }
                }
            }
            return addDeficitDetail;

        }
        return editDeficitBase;
    }
}
